//
//  NetworkingTools.m
//  autoMasterTM
//
//  Created by my-pc on 16/5/23.
//  Copyright © 2016年 qianjinding. All rights reserved.
//

#import "NetworkingTools.h"
#import "AFNetworking.h"

@interface NetworkingTools()

@end

@implementation NetworkingTools


/**
 网络获取数据
 */

+(void)getDataWithURL:(NSString *)urlStr sign:(NSString *)sign Block:(AppToolBlock)block ErrorBlock:(AppToolErrorBlock)errorBlock
{
//    NSError *error2 = nil;
    NSString *urlEncode = [urlStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSString *encodedUrl1 = (NSString*)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(nil,
                                                                                                 (CFStringRef)sign, nil,
                                                                                                 (CFStringRef)@"!*'();:@&=+$,/?%#[]", kCFStringEncodingUTF8));

    NSString *url = [NSString stringWithFormat:@"%@&sign=%@",urlEncode,encodedUrl1];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    //可能返回的不是Json，也得做出反应
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];// 请求
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];// 响应
    NSString *session = [[NSUserDefaults standardUserDefaults]objectForKey:@"session"];
    NSString *value = [NSString stringWithFormat:@"PHPSESSID=%@",session];
    [manager.requestSerializer setValue:value forHTTPHeaderField:@"Cookie"];
    [manager GET:url parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        //block回调
        NSLog(@"====responseObject%@",responseObject);
        NSString *str=[[NSString alloc]initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"====%@",str);
        block(str);
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        errorBlock(error);
        NSLog(@"失败==== %@",error);
        
    }];
    
}

/**
 发送POST请求
 */

+(void)post:(NSString*)url params:(NSDictionary *)params success:(void(^)(id responseObj))success failure:(void(^)(NSError *error))failure
{
    
    
    
    NSString *urlEncode = [url stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    //1.获得请求
     AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    
   NSString *session = [[NSUserDefaults standardUserDefaults]objectForKey:@"session"];
    
//    TZLog(@"---sesson--%@",session);
    
    NSString *value = [NSString stringWithFormat:@"PHPSESSID=%@",session];
    [manager.requestSerializer setValue:value forHTTPHeaderField:@"Cookie"];
    
    //2.发送GET请求
    [manager POST:urlEncode parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
       
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (success) {
            
            NSDictionary *Data = responseObject;
            NSString *errorcode = Data[@"error_code"];
            
            
            if ([errorcode isEqualToString:@"901"]) {
                
//                TZLog(@"---Data = %@",responseObject);
                

                //1.重新登陆
//                [NetworkingTools loginPost];
                
                //2.
//                [NetworkingTools post:url params:params success:success   failure:failure];
                
            }else{
            
                success(responseObject);
                
              
            }
            
            
            
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        failure(error);
        
        
    }];
    
}


@end






























